﻿using DEQI_Aftermarket.SqlConn;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DEQI_Aftermarket.Controllers
{
    public class DataExchangeController : Controller
    {
        // GET: DataExchange

        BaseDao dao = new BaseDao();
        public ActionResult Index()
        {
            return View();
        }

        //实现统计用户人数
        public ActionResult GetUserNum()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);

            return Json('0');
        }


        public ActionResult Table_User()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";

                 
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,user_id,user_name,user_phone,user_type" +
                        " FROM t_user " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "rowNumber", "user_id", "user_name", "user_phone", "user_type" });


                SqlNum = "select count(user_id) as TotalNum  FROM t_user  where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /DataExchange/Table_User:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }

        public ActionResult Table_UserAdd()
        {
            string UserName = Request.Form["user"] == null ? null : Request.Form["user"].ToString();
            string CompanyName = Request.Form["company"] == null ? null : Request.Form["company"].ToString();
            string UserPhone = Request.Form["phone"] == null ? null : Request.Form["phone"].ToString();
            string UserAddress = Request.Form["address"] == null ? null : Request.Form["address"].ToString();
            string UserEmail = Request.Form["email"] == null ? null : Request.Form["email"].ToString();
            string UserType = Request.Form["u_type"] == null ? null : Request.Form["u_type"].ToString();
            string UserPwd = Request.Form["password"] == null ? null : Request.Form["password"].ToString();
            Dictionary<string, object> result = new Dictionary<string, object>();
            if (UserName == null)
            {
                result.Add("result", "error");
                return Json(result);
            }
            return Json(result);
        }


        public ActionResult UserAdd()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);
            sr.Close();
            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                Dictionary<string, object> map = new Dictionary<string, object>();

                foreach (var item in obj)
                {
                    map.Add(item.Key, item.Value);
                }
                map["IsDelete"] = "0";
                map["user_pwd"] = "123456";
                if ("" != dao.GetString("t_User", "user_name", "and user_name='" + map["user_name"] + "'"))
                {
                    result.Add("result", "用户名已存在");
                    return Json(result);
                }
                dao.save("t_User", map);
                result.Add("result", "OK");
                return Json(result);
            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);
            }
        }

        public ActionResult Table_UserDel()
        {
            string UserId = Request.Form["user_id"] == null ? null : Request.Form["user_id"].ToString();
            Dictionary<string, object> result = new Dictionary<string, object>();
            if (UserId == null)
            {
                result.Add("result", "error");
                return Json(result);
            }

            string sql_List = "update  t_user set IsDelete=1  where user_id=" + UserId;

            try
            {
                BaseDao.execute(sql_List);
                result.Add("result", "OK");
                return Json(result);
            }
            catch (Exception e)
            {

                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);
            }
        }


        public ActionResult Table_Maintain()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";


                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,maintain_id,maintain_no,product_name,product_model,problem_description,state" +
                        " FROM t_maintain " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "rowNumber", "maintain_id", "maintain_no", "product_name", "product_model", "problem_description", "state" });


                SqlNum = "select count(maintain_id) as TotalNum  FROM t_maintain  where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /DataExchange/Table_User:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }

        public ActionResult DjAdd()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);
            sr.Close();
            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                Dictionary<string, object> map = new Dictionary<string, object>();

                foreach (var item in obj)
                {
                    map.Add(item.Key, item.Value);
                }
                map["IsDelete"] = "0";

                var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
                long times = Convert.ToInt64(ts.TotalMilliseconds);
                map["maintain_no"] = "WXD_" + times.ToString().Substring(times.ToString().Length-7,7);

                map["creat_time"] = DateTime.Now.ToString();
                map["update_time"] = DateTime.Now.ToString();
                map["state"] = "0";
                if ("" != dao.GetString("t_maintain", "maintain_no", "and maintain_no='" + map["maintain_no"] + "'"))
                {
                    result.Add("result", "单据已存在");
                    return Json(result);
                }
                dao.save("t_maintain", map);
                result.Add("result", "OK");
                return Json(result);
            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);
            }
        }

        public ActionResult Table_DjDel()
        {
            string MsintainId = Request.Form["maintain_id"] == null ? null : Request.Form["maintain_id"].ToString();
            Dictionary<string, object> result = new Dictionary<string, object>();
            if (MsintainId == null)
            {
                result.Add("result", "error");
                return Json(result);
            }

            string sql_List = "update t_maintain set IsDelete=1  where maintain_id=" + MsintainId;

            try
            {
                BaseDao.execute(sql_List);
                result.Add("result", "OK");
                return Json(result);
            }
            catch (Exception e)
            {

                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);
            }
        }

    }
}